﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace BigLotterySyndicate.Classes
{
    public class TimePicker : Control, INamingContainer
    {

        private string m_ControlCssClass ="";
        private string m_CssHours ="";
        private string m_CssMinutes ="";
        
        private string m_TabIndexHours ="0";
        private string m_TabIndexMinutes = "0";

        private string m_SelectedHour ="";
        private string m_SelectedMinute ="";

        public string CssClassHours
        {
            get
            {
                if (this.Controls.Count == 0)
                    return "";
                return ((System.Web.UI.WebControls.DropDownList)Controls[0]).CssClass;
            }
            set
            {
                if (this.Controls.Count != 0)
                {
                    ((System.Web.UI.WebControls.DropDownList)Controls[0]).CssClass = value;
                    m_CssHours = "";
                }
                else
                    m_CssHours = value;
            }
        }

        public string CssClassMinutes
        {
            get
            {
                if (this.Controls.Count == 0)
                    return "";
                return ((System.Web.UI.WebControls.DropDownList)Controls[1]).CssClass;
            }
            set
            {
                if (this.Controls.Count != 0)
                {
                    ((System.Web.UI.WebControls.DropDownList)Controls[1]).CssClass = value;
                    m_CssMinutes = "";
                }
                else
                    m_CssMinutes = value;
            }
        }
        
        public string TabIndexHours
        {
            get
            {
                if (this.Controls.Count == 0)
                    return "0";
                return ((System.Web.UI.WebControls.DropDownList)Controls[0]).TabIndex.ToString();
            }
            set
            {
                if (this.Controls.Count != 0)
                {
                    ((System.Web.UI.WebControls.DropDownList)Controls[0]).TabIndex = Int16.Parse(value);
                    m_TabIndexHours = "0";
                }
                else
                    m_TabIndexHours = value;
            }
        }

        public string TabIndexMinutes
        {
            get
            {
                if (this.Controls.Count == 0)
                    return "0";
                return ((System.Web.UI.WebControls.DropDownList)Controls[2]).TabIndex.ToString();
            }
            set
            {
                if (this.Controls.Count != 0)
                {
                    ((System.Web.UI.WebControls.DropDownList)Controls[2]).TabIndex = Int16.Parse(value);
                    m_TabIndexMinutes = "0";
                }
                else
                    m_TabIndexMinutes = value;
            }
        }

        public string SelectedHour
        {
            get
            {
                if (this.Controls.Count == 0)
                    return "";
                return ((System.Web.UI.WebControls.DropDownList)Controls[0]).SelectedValue;
            }
            set
            {
                if (this.Controls.Count != 0)
                {
                    ((System.Web.UI.WebControls.DropDownList)Controls[0]).SelectedValue = value;
                    m_SelectedHour = "";
                }
                else
                    m_SelectedHour = value;
            }
        }

        public string SelectedMinute
        {
            get
            {
                if (this.Controls.Count == 0)
                    return "";
                return ((System.Web.UI.WebControls.DropDownList)Controls[2]).SelectedValue;
            }
            set
            {
                if (this.Controls.Count != 0)
                {
                    ((System.Web.UI.WebControls.DropDownList)Controls[2]).SelectedValue = value;
                    m_SelectedMinute = "";
                }
                else
                    m_SelectedMinute = value;
            }
        }

        public bool Enabled
        {
            get;
            set;
        }

        public string ControlCssClass
        {
            get { return m_ControlCssClass; }
            set { m_ControlCssClass = value; }
        }

        public TimePicker()
        {
            this.Enabled = true;
            //
            // TODO: Add constructor logic here
            //
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            DropDownList listHours = new DropDownList();
            listHours.ID = "pickHours";

            if (m_CssHours != "")
                listHours.CssClass = m_CssHours;

            if (m_TabIndexHours != "0")
                listHours.TabIndex = Int16.Parse(m_TabIndexHours);


            string counter1 = "";
            for (int i = 0; i <= 23; i++)
            {
                if (i < 10)
                    counter1 = "0" + i.ToString();
                else
                    counter1 = i.ToString();



                listHours.Items.Add(new ListItem(counter1.ToString()));
            }

            if (this.m_SelectedHour != "")
                listHours.SelectedValue = m_SelectedHour;

            DropDownList listMinutes = new DropDownList();
            listMinutes.ID = "pickMinutes";

            if (m_CssMinutes != "")
                listMinutes.CssClass = m_CssMinutes;

            if (m_TabIndexMinutes != "0")
                listMinutes.TabIndex = Int16.Parse(m_TabIndexMinutes);
            string counter2 = "";
            for (int i = 0; i <= 59; i++)
            {
                if (i < 10)
                    counter2 = "0" + i.ToString();
                else
                    counter2 = i.ToString();

                listMinutes.Items.Add(new ListItem(counter2));
            }

            if (this.m_SelectedMinute != "")
               listMinutes.SelectedValue = m_SelectedMinute;


            var span = new HtmlGenericControl("span");
            span.InnerHtml = ":";

            listMinutes.Enabled = this.Enabled;
            listHours.Enabled = this.Enabled;


            this.Controls.Add(listHours);
            this.Controls.Add(span);
            this.Controls.Add(listMinutes);
        }

    }
}
